/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
  useTheme, CCell, Table, TableBody, TableRow, Text, RCell, Cell, TableHead,
} from "customize-easy-ui-component";
import { DirectLink, } from "../../../routing/Link";
import {getInstrument2xCol, } from "../../common/helper";
import {floatInterception,} from "../../../common/tool";
import {检验编制核准Eval} from "../rarelyVary";
import {useThreeRaftSurveyTbl} from "../../hook/useThreeRaft";
import {config设备概况} from "./orcBase";
import {useJudgmentTable, useJudgmentTableX} from "../../hook/useJudgment";
import {RepLink} from "../../common/base";

/**首页
 * @param original 是否为了打印正式版原始记录
 * */
export const 首页设备概况= ({theme, orc, original,rep} :{theme: any, orc:any, original?:boolean,rep:any}
) => {
  return  <Table fixed={ ["20%","%"] }  css={ {borderCollapse: 'collapse'} }>
    <TableBody rheight={40}>
      <TableRow>
        <RCell css={{border:'none'}}>委托单位：</RCell>
        <CCell css={{border:'none',borderBottom:`1px dashed`}}>{orc.使用单位}</CCell>
      </TableRow>
      <TableRow >
        <RCell css={{border:'none'}}>设备名称：</RCell>
        <CCell css={{border:'none',borderBottom:`1px dashed`}}>{orc?.设备名称}</CCell>
      </TableRow>
      <TableRow >
        <RCell css={{border:'none'}}>报告编号：</RCell>
        <CCell css={{border:'none',borderBottom:`1px dashed`}}>{rep.isp?.no}</CCell>
      </TableRow>
      <TableRow>
        <RCell css={{border:'none'}}>设  备  号：</RCell>
        <CCell css={{border:'none',borderBottom:`1px dashed`}}>{orc.eqpcod}</CCell>
      </TableRow>
      <TableRow>
        <RCell css={{border:'none'}}>评估日期：</RCell>
        <CCell css={{border:'none',borderBottom:`1px dashed`}}>{orc.检验日期}</CCell>
      </TableRow>
    </TableBody>
  </Table>;
};

/**正式报告用的
 * */
export const 报告设备详情= ( { orc,  repId, verId, rep } : { orc: any,repId: string,verId: string,rep:any}
) => {
  // const instrumentTable =React.useMemo(() => getInstrument2xCol(orc.仪器表), [orc.仪器表]);
  const renderUpper=useThreeRaftSurveyTbl({config: config设备概况,
                 orc, rep, slash:true});
  return <React.Fragment>
      <Table id={'Survey'} fixed={ ["8.1%","27%","19%","12.1%","9%","%"] }  css={ {borderCollapse: 'collapse' } }  tight  miniw={800}>
        <TableBody>
          <DirectLink  href={`/report/WALK-SAF/ver/${verId}/${repId}/Survey#Survey`}>
            {renderUpper}
          </DirectLink>
        </TableBody>
      </Table>
  </React.Fragment>;
};

export const 运行速度偏差= ({theme, orc, rep} :{theme: any, orc:any, rep:any}
) => {
  const speedVs= floatInterception(orc?.行距离 / orc?.用时间, 4) as unknown as number;
  const resPec=floatInterception(100*(speedVs-orc?.运行速度)/orc?.运行速度, 2);
  const resPec2=floatInterception(100*(orc?.仪速度偏-orc?.运行速度)/orc?.运行速度, 2);
  return <>
    <div css={{"@media print": {paddingBottom: '4.5rem', pageBreakInside: 'avoid'}}}>
      <Text id={'Limiter'} variant="h4" css={{marginTop: '1rem',
      }}>附录A 空载梯级（踏板、胶带）运行速度与名义速度偏差（简易方法）</Text>
    </div>
    <Table fixed={ ["27%", "%", "32%"] }
           css={{borderCollapse: 'collapse', "@media print": {marginTop: '-4.5rem'}}} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'SpeedBias'}>
          <TableRow><CCell>项目编号</CCell><CCell colSpan={2}>42.7</CCell></TableRow>
          <TableRow><CCell>检验内容</CCell><CCell>梯级、踏板或胶带实际运行距离S(m)</CCell><CCell>运行距离所用的时间t(s)</CCell></TableRow>
          <TableRow><CCell>观测数据</CCell><CCell>{orc.行距离 ?? '／'} m</CCell><CCell>{orc.用时间 ?? '／'} s</CCell></TableRow>
          <TableRow><CCell>梯级、踏板或胶带运行速度Vs</CCell><CCell colSpan={2}>{speedVs ?? '／'} m/s</CCell></TableRow>
          <TableRow><CCell>计算结果：</CCell><CCell colSpan={2}>{resPec ?? '／'} %</CCell></TableRow>
          <TableRow><CCell>判断标准</CCell><CCell colSpan={2}>-5%≤(Vs-Vn)/Vn≤5%</CCell></TableRow>
          <TableRow><CCell>检验结果</CCell><CCell colSpan={2}>{orc.速差r ?? '／'}</CCell></TableRow>
        </RepLink>
      </TableBody>
    </Table>

    <div css={{"@media print": {paddingBottom: '3.5rem', pageBreakInside: 'avoid'}}}>
      <Text variant="h5" css={{marginTop: '1rem',
      }}>空载梯级（踏板、胶带）运行速度与名义速度偏差（自动扶梯综合检测仪等其他仪器）</Text>
    </div>
    <Table fixed={ ["27%", "%", "32%"] }
           css={{borderCollapse: 'collapse', "@media print": {marginTop: '-3.5rem'}}} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'SpeedBias'}>
          <TableRow><CCell>项目编号</CCell><CCell colSpan={2}>42.7</CCell></TableRow>
          <TableRow><CCell>梯级、踏板或胶带运行速度Vs</CCell><CCell colSpan={2}>{orc?.仪速度偏 ?? '／'} m/s</CCell></TableRow>
          <TableRow><CCell>计算结果：</CCell><CCell colSpan={2}>{resPec2 ?? '／'} %</CCell></TableRow>
          <TableRow><CCell>判断标准</CCell><CCell colSpan={2}>-5%≤(Vs-Vn)/Vn≤5%</CCell></TableRow>
          <TableRow><CCell>检验结果</CCell><CCell colSpan={2}>{orc.仪速差r ?? '／'}</CCell></TableRow>
        </RepLink>
      </TableBody>
    </Table>
    <Text css={{fontSize: '0.7rem'}}>
      备注：速度偏差计算结果的判定，当空载梯级（踏板、胶带）运行速度大于名义速度时为正值，反之为负值。
    </Text>
  </>;
};

export const 扶手带速度偏差= ({theme, orc, rep} :{theme: any, orc:any, rep:any}
) => {
  return <>
    <div css={{"@media print": {paddingBottom: '4.5rem', pageBreakInside: 'avoid'}}}>
      <Text id={'HandrailBias'} variant="h4" css={{marginTop: '1rem',
      }}>附录B 空载梯级（踏板、胶带）和扶手带运行速度偏差（简易方法）</Text>
    </div>
    <Table fixed={ ["20%","27%","12%","13%","%"] }
           css={{borderCollapse: 'collapse', "@media print": {marginTop: '-4.5rem'}}} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'HandrailBias'}>
          <TableRow><CCell>项目编号</CCell><CCell colSpan={4}>42.8</CCell></TableRow>
          <TableRow><CCell>检验内容</CCell><CCell>实际运行距离S(mm)</CCell>
            <CCell colSpan={2}>左侧扶手带与梯级（踏板、胶带）错位距离Sl(mm)</CCell><CCell>右侧扶手带与梯级（踏板、胶带）错位距离Sr(mm)</CCell>
          </TableRow>
          <TableRow><CCell>观测数据</CCell><CCell>{orc.扶行距离 ?? '／'} mm</CCell>
            <CCell colSpan={2}>{orc.左错位距 ?? '／'} mm</CCell><CCell>{orc.右错位距 ?? '／'} mm</CCell>
          </TableRow>
          <TableRow><CCell rowSpan={2}>计算结果：</CCell><CCell colSpan={2}>左侧扶手带速度偏差δl；δl=Sl/S</CCell><CCell colSpan={2}>右侧扶手带速度偏差δr；δr=Sr/S</CCell></TableRow>
          <TableRow><CCell colSpan={2}>{floatInterception(100*orc?.左错位距/orc?.扶行距离, 2)} %</CCell>
            <CCell colSpan={2}>{floatInterception(100*orc?.右错位距/orc?.扶行距离, 2)} %</CCell>
          </TableRow>
          <TableRow><CCell>判断标准</CCell><CCell colSpan={4}>允许偏差为0-+2%</CCell></TableRow>
          <TableRow><CCell>检验结果</CCell><CCell colSpan={4}>{orc.扶速差r ?? '／'}</CCell></TableRow>
        </RepLink>
      </TableBody>
    </Table>

    <div css={{"@media print": {paddingBottom: '3.5rem', pageBreakInside: 'avoid'}}}>
      <Text variant="h5" css={{marginTop: '1rem',
      }}>空载梯级（踏板、胶带）和扶手带运行速度偏差（自动扶梯综合检测仪等其他仪器）</Text>
    </div>
    <Table fixed={ ["22%","39%","%"] }
           css={{borderCollapse: 'collapse', "@media print": {marginTop: '-3.5rem'}}} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'HandrailBias'}>
          <TableRow><CCell>项目编号</CCell><CCell colSpan={2}>42.8</CCell></TableRow>
          <TableRow><CCell rowSpan={2}>计算结果：</CCell><CCell>左侧扶手带速度偏差δl；δl=Sl/S</CCell><CCell>右侧扶手带速度偏差δr；δr=Sr/S</CCell></TableRow>
          <TableRow><CCell>{orc?.仪左错距?? '／'} %</CCell><CCell>{orc?.仪右错距?? '／'} %</CCell></TableRow>
          <TableRow><CCell>判断标准</CCell><CCell colSpan={2}>允许偏差为0-+2%</CCell></TableRow>
          <TableRow><CCell>检验结果</CCell><CCell colSpan={2}>{orc.仪扶差r ?? '／'}</CCell></TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};

export const 运行振动评价表= ( { orc,  repId, verId, rep } : { orc: any,repId: string,verId: string, rep: any}
) => {
  const theme = useTheme();
  return <>
    <div css={{"@media print": {paddingBottom: '7rem', pageBreakInside: 'avoid'}}}>
      <Text id={'VibrationEval'} variant="h4" css={{marginTop: '1rem',
      }}>附录C 自动扶梯和自动人行道运行振动评价表</Text>
    </div>
    <Table fixed={["%"]} css={{borderCollapse: 'collapse', "@media print": {marginTop: '-7rem'}}} tight miniw={800}>
      <TableBody>
          <TableRow><CCell>运载装置振动测量结果</CCell></TableRow>
      </TableBody>
    </Table>
    <Table fixed={ ["4%", "15%", "12%", "14%","14%", "14%", "14%", "%"] }
              css={{borderCollapse: 'collapse', }} tight miniw={800}>
      <TableHead>
          <TableRow>
            <CCell rowSpan={2}>序号</CCell><CCell rowSpan={2}>测量速度m/s</CCell><CCell rowSpan={2}>运行方向</CCell>
            <CCell colSpan={4}>r.m.s 值 m/s²</CCell><CCell rowSpan={2}>评价等级</CCell>
          </TableRow>
          <TableRow><CCell>x轴</CCell><CCell>y轴</CCell><CCell>z轴</CCell><CCell>矢量和</CCell></TableRow>
      </TableHead>
      <TableBody>
        <RepLink rep={rep} tag={'VibrationEval'}>
          { (orc?.振动表??[null,])?.map((o: any, i: number) => {
            return (
                <TableRow key={i}>
                  <CCell>{i+1}</CCell><CCell>{o?.s??'/'}</CCell><CCell>{o?.f??'/'}</CCell>
                  <CCell>{o?.x??'/'}</CCell><CCell>{o?.y??'/'}</CCell><CCell>{o?.z??'/'}</CCell>
                  <CCell>{o?.vs??'/'}</CCell><CCell>{o?.L??'/'}</CCell>
                </TableRow>
            );
          } ) }
        </RepLink>
      </TableBody>
    </Table>
    <Table fixed={ ["19%", "26%", "28%", "%"] }
           css={{borderCollapse: 'collapse', }} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'VibrationEval'}>
          <TableRow>
            <CCell>最大r.m.s 值：</CCell><CCell>{orc?.大rms??'/'}</CCell>
            <CCell>平均r.m.s 值：</CCell><CCell>{orc?.均rms??'/'}</CCell>
          </TableRow>
          <TableRow><CCell colSpan={4}>扶手带振动测量结果</CCell></TableRow>
        </RepLink>
      </TableBody>
    </Table>
    <Table fixed={ ["4%", "15%", "12%", "14%","14%", "14%", "14%", "%"] }
           css={{borderCollapse: 'collapse', }} tight miniw={800}>
      <TableHead>
        <TableRow>
          <CCell rowSpan={2}>序号</CCell><CCell rowSpan={2}>测量速度m/s</CCell><CCell rowSpan={2}>运行方向</CCell>
          <CCell colSpan={2}>位置</CCell><CCell rowSpan={2} colSpan={2}>r.m.s 值 m/s²</CCell><CCell rowSpan={2}>评价等级</CCell>
        </TableRow>
        <TableRow><CCell>左侧</CCell><CCell>右侧</CCell></TableRow>
      </TableHead>
      <TableBody>
        <RepLink rep={rep} tag={'HandrailVibEval'}>
          { (orc?.扶振表??[null,])?.map((o: any, i: number) => {
            return (
                <TableRow key={i}>
                  <CCell>{i+1}</CCell><CCell>{o?.s??'/'}</CCell><CCell>{o?.f??'/'}</CCell>
                  <CCell>{o?.l??'/'}</CCell><CCell>{o?.r??'/'}</CCell><CCell>{o?.o??'/'}</CCell>
                  <CCell>{o?.t??'/'}</CCell><CCell>{o?.L??'/'}</CCell>
                </TableRow>
            );
          } ) }
        </RepLink>
      </TableBody>
    </Table>
    <Table fixed={ ["19%", "26%", "28%", "%"] } css={{borderCollapse: 'collapse', }} tight miniw={800}>
      <TableBody>
        <RepLink rep={rep} tag={'HandrailVibEval'}>
          <TableRow>
            <CCell>最大r.m.s 值：</CCell><CCell>{orc?.扶大rm??'/'}</CCell>
            <CCell>平均r.m.s 值：</CCell><CCell>{orc?.扶均rm??'/'}</CCell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};

export const 依据检测仪器 = ({orc, repId, verId, rep}: { orc: any, repId: string, verId: string, rep: any }
) => {
  const theme = useTheme();
  const instrumentTable = React.useMemo(() => getInstrument2xCol(orc.仪器表), [orc.仪器表]);
  return <React.Fragment>
    <Text variant="h4" css={{
      "@media print": {
        marginTop: '1rem',
      },
    }}>二、评估依据</Text>
    1、GB 16899 --2011 《自动扶梯和自动人行道的制造与安装安全规范》<br/>
    2、GB/T 20900-2007 《电梯、自动扶梯和自动人行道风险评价和降低的方法》<br/>
    3、GB/T 30692 --2014 《提高在用自动扶梯与自动人行道安全性的规范》<br/>
    4、GB/T 37217 --2018《自动扶梯和自动人行道主要部件报废技术条件》<br/>
    5、TSG T7005 --2012 《电梯监督检验和定期检验规则 -- --自动扶梯与自动人行道》(含1号、2号、3 号修改单)<br/>
    6、DB35/T 1936—2020 自动扶梯和自动人行道运行振动测量和评价
    <Text variant="h4" css={{
      "@media print": {
        marginTop: '1rem',
      },
    }}>三、主要检测仪器</Text>
    <Table fixed={["6%", "15%", "28%", "13%", "17%", "%"]}
           css={{borderCollapse: 'collapse', height: 'fill-available'}} tight miniw={800}>
      <TableBody>
        <DirectLink href={`/report/WALK-SAF/ver/${verId}/${repId}/Instrument`}>
          <TableRow>
            <CCell>序号</CCell>
            <CCell>仪器名称</CCell>
            <CCell>仪器编号</CCell>
            <CCell>序号</CCell>
            <CCell>仪器名称</CCell>
            <CCell>仪器编号</CCell>
          </TableRow>
          {instrumentTable.map((o, i) => {
            return (
                <TableRow key={i}>
                  <CCell>{o.s1}</CCell>
                  <CCell>{o.name1}</CCell>
                  <CCell css={{wordBreak: 'break-all'}}>{o.no1}</CCell>
                  <CCell>{o.s2}</CCell>
                  <CCell>{o.name2}</CCell>
                  <CCell css={{wordBreak: 'break-all'}}>{o.no2}</CCell>
                </TableRow>
            );
          })
          }
        </DirectLink>
      </TableBody>
    </Table>
  </React.Fragment>;
};

export const 评估综述结论= ( { orc,  repId, verId, rep, evalTotalScore} : { orc: any,repId: string,verId: string,rep:any,evalTotalScore?:string}
) => {
  const theme= useTheme();
  const instrumentTable =React.useMemo(() => getInstrument2xCol(orc.仪器表), [orc.仪器表]);
  return <React.Fragment>
    <Text variant="h4" css={{
      "@media print": {
        marginTop: '1rem',
      },
    }}>四、评估结论</Text>
    <Table fixed={ ["5.5%","%"] }  css={ {borderCollapse: 'collapse' } }  tight  miniw={800}>
      <TableBody>
        <TableRow >
          <CCell rowSpan={3} split={true}>评估综述</CCell>
          <Cell><Text css={{textIndent: '2rem', display: 'flex',}}>
            (一) 自动扶梯(自动人行道)状况：通过对该自动扶梯(自动人行道)的安全性能技术评估，该自动扶梯(自动人行道)设备本体安全状况分值为 {evalTotalScore} 分，
            自动扶梯(自动人行道)安全级别整体水平为“ {orc.级别整} ”级， 自动扶梯(自动人行道)本体状态“ {orc.本体状} ”。
          </Text></Cell>
        </TableRow>
        <TableRow >
          <Cell><Text css={{textIndent: '2rem', display: 'flex',}}>
            (二)存在的风险：
          </Text>
            <Text css={{textIndent: '2rem', display: 'flex',}}>
              1、存在 {orc?.unqA?.length || 0 } 个不符合项目，这些项目不符合《电梯监督检验和定期检验规则—
              自动扶梯与自动人行道》（TSG T7005-2012）(含1号、2号、3号修改单)，已存在一定
              安全风险，必须整改（详见表1）。以上项目未经整改合格，该电梯严禁投入使用；
            </Text>
            <Text css={{textIndent: '2rem', display: 'flex',}}>
              2、存在 {orc?.unq?.length || 0 } 个不符合项目，虽然能够符合现行安全技术规范要求，但其安全技
              术性能降低，具有一定的潜在安全风险，对以上项目建议整改（详见表2）。在整改完成
              前，维保单位应当加强日常维护保养，使用单位应当采取有效的安全监护措施；
            </Text>
            <Text css={{textIndent: '2rem', display: 'flex',}}>
              3、存在 {orc?.unqB?.length || 0 } 个不符合项目，该自动扶梯（自动人行道）执行旧的安全技术规范
              和标准，因为新旧安全技术规范或标准的要求不一致，导致这些项目不符合现行安全技
              术规范或标准的要求（详见表3）。
            </Text>
          </Cell>
        </TableRow>
        <TableRow >
          <Cell><Text css={{textIndent: '2rem', display: 'flex', whiteSpace: 'pre-wrap'}}>
            (三)、使用维保建议：<br/>
            {orc?.建议 || '／'}
          </Text>
          </Cell>
        </TableRow>

        <DirectLink  href={`/report/WALK-SAF/ver/${verId}/${repId}/Conclusion#Conclusion`}>
          <TableRow>
            <CCell>评估结论</CCell>
            <Cell><div css={{minHeight: '3rem', whiteSpace: 'pre-wrap'}}>
              <Text css={{fontSize: '1rem',fontWeight: 800}}>{orc?.评估结论 || '／'}
              </Text></div>
           </Cell>
        </TableRow>
        <TableRow>
          <CCell>备注</CCell>
          <Cell><div css={{whiteSpace: 'pre-wrap'}}>
              <Text>{orc?.大备注 || '／'}
              </Text></div>
          </Cell>
        </TableRow>
        </DirectLink>
      </TableBody>
    </Table>
    {检验编制核准Eval({orc, rep})}
  </React.Fragment>;
};

//需拆分成两个编辑器的情况：测量表项目太多；Measure Measure2路由标签； 和编辑器布局没有直接关联。
export const 测量记录两半= ({ orc, rep,label,config1,config2,children,fixed=["2%", "5%", "3%", "6%", "%", "8%", "5%", "9%", "7%", "5%"],nseq,crit}
                    : {orc: any,rep: any,label:string,config1:any,config2:any,fixed?:string[],children?:any,nseq?:boolean,crit?:boolean}
) => {
  const {rows: render1,seq}=useJudgmentTableX({rep,orc, config:config1});
  const render2=useJudgmentTable({rep,orc, config:config2,seqOfs:seq,tag:'Measure2'});
  return <>
    <Text variant="h4" css={{marginTop: '1rem',
    }}>{label}</Text>
    <Table id={'Measure'} fixed={ fixed }  css={ {borderCollapse: 'collapse' } } tight  miniw={800}>
      <TableHead>
        <TableRow>
          <CCell><Text css={{fontSize:'0.75rem'}}>序号</Text></CCell><CCell colSpan={2}><Text css={{fontSize:'0.75rem'}}>项目编号</Text></CCell>
          <CCell colSpan={2}>检验内容与要求</CCell><CCell>检测项目</CCell><CCell>单位</CCell>
          <CCell>观测数据</CCell><CCell>测量结果</CCell><CCell><Text css={{fontSize:'0.75rem'}}>结果判定</Text></CCell>
        </TableRow>
      </TableHead>
      <TableBody>
        <DirectLink  href={`/report/${rep?.modeltype}/ver/${rep?.modelversion}/${rep?.id}/Measure?original=1#Measure`}>
          {render1}
        </DirectLink>
        <DirectLink  href={`/report/${rep?.modeltype}/ver/${rep?.modelversion}/${rep?.id}/Measure2?original=1#Measure2`}>
          {render2}
        </DirectLink>
      </TableBody>
    </Table>
    {children ? children :
        <Text css={{fontSize:'0.8rem'}}>
          注：本表所列项目无测量时，观测数据和测量结果可不填，但结果判定应填写，对不适用项填“/”。
        </Text>
    }
  </>;
};


/*特殊布局：<p>段落区域也会自动协调的？
      <Text css={{
        textIndent: '2rem',
        display: 'flex',
      }}>
        <p> 1、111存在 个不符合项目，这些项目不符合《；</p>
        2、222存改（成前，维保单位应当加效的安全监护措施；<br/>
        3、333存在的发或标准见表3）。
      </Text>
* */
